/**
 * Project oo_thread
 */


#include "../include/MySQLConnector.h"
#include "../include/LoginGetusername.h"
#include <ctime>
#include <stdlib.h>
#include <iostream>
#include <unistd.h>
using std::endl;
using std::cout;
enum TaskType{
    TASK_LOGIN_SECTION1 = 1, 
    TASK_LOGIN_SECTION1_RESP_OK,
    TASK_LOGIN_SECTION1_RESP_ERROR,
    TASK_LOGIN_SECTION2,
    TASK_LOGIN_SECTION2_RESP_OK,
    TASK_LOGIN_SECTION2_RESP_ERROR,
    TASK_TYPE_REGISTER1,
    TASK_TYPE_REGISTER1_RESP_OK,
    TASK_TYPE_REGISTER1_RESP_ERROR,
    TASK_TYPE_GEGISTER2,
    TASK_TYPE_REGISTER2_RESP_OK
};   

/**
 * RegisterTask implementation
 */
LoginGetusername::LoginGetusername(const string msg, TcpConnectionPtr conn)
:_msg(msg)
,_conn(conn)
{

}

/**
 * @return void
 */
void LoginGetusername::process() {
    // cout << "RegisterTask.cpp 28 process(),msg = " << _msg << endl;

    //handle msg
    cout << "LoginGetusername handle msg" << endl;
    sleep(1);
    // string sql = "select * from user where name = '" + _msg + "'";
    MySQLConnector* mysqlconn = MySQLConnector::getInstance();
    string sql = "select * from user where name = '" + _msg + "'";
    LOG_WARN("forbit combining sql string, please conrrect later!!");
    cout << "sql = " << sql << endl;
    mysqlconn->query(sql);
    if(mysqlconn->hasResult())
    {
    cout << "RegisterGetusername.cpp 78, process(), already hasResult" << endl;
    mysqlconn->showResult();
    string salt = mysqlconn->getSalt();
    _conn->sendtoLoop(TASK_LOGIN_SECTION1_RESP_OK
                      ,salt);
    }
    else
    {
    _conn->sendtoLoop(TASK_LOGIN_SECTION1_RESP_ERROR
                      ,"username not exist");
    }
}
